package com.puyuba.puyuba;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;


import com.alibaba.android.arouter.facade.annotation.Route;
import com.pyb.java.basemodule.BaseActivity;
import com.pyb.java.cache.CacheUserInfo;
import com.puyuba.puyuba.view.fragment.HomeTabManager;
import com.puyuba.puyuba.view.widgets.MyTabView;
import com.pyb.java.commonutil.activity.ActivityManager;
import com.pyb.java.commonutil.constant.RouterActivityPath;
import com.pyb.java.commonutil.utils.StatusBarUtil;

@Route(path = RouterActivityPath.Main.PAGER_MAIN)
public class MainActivity extends BaseActivity implements MyTabView.OnTabSelectedListener {

    private static final String TAG = MainActivity.class.getSimpleName();

    private MyTabView mTabWidget;
    private FragmentManager mFragmentManager;
    private Fragment mContentFragment;
    public int mIndex = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTabWidget = findViewById(R.id.tab_widget);

        initTab();
        initview();
    }

    public static void startActivity(Context context){
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    private void initTab() {
        mTabWidget.setOnTabSelectedListener(this);
    }

    private void initview() {
        StatusBarUtil.setTransparentForImageViewInFragment(ActivityManager.last(), null);
        mFragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        mContentFragment = HomeTabManager.getInstance().getFragmentByIndex(ConstantValues.FIND_INDEX);
        mTabWidget.removeAllViews();
        mTabWidget.init(this);
        transaction.add(R.id.fl_home_content, mContentFragment);
        transaction.commitAllowingStateLoss();

        setTabsDisplay(mIndex);
        //如果FindFragment用H5要在这存下数据 如果用原生写 就不用在这保存
        if (CacheUserInfo.getInstance().isFindWeb())setPage();
    }

    //如果要展示申请人弹窗，切换tab
    public void setPage(){
        setTabsDisplay(1);
        switchFragment(HomeTabManager.getInstance().getFragmentByIndex(1), 1);
    }

    public void setTabsDisplay(int index) {
        mTabWidget.setTabsDisplay(this, index, 0);
    }

    private void switchFragment(Fragment to, int index) {
        if (mContentFragment != to) {
            FragmentTransaction transaction = mFragmentManager.beginTransaction().setCustomAnimations(R.anim.home_fade_in, R.anim.home_fade_out);
            if (!to.isAdded()) {    // 先判断是否被add过
                transaction.hide(mContentFragment).add(R.id.fl_home_content, to, index + "").commitAllowingStateLoss(); // 隐藏当前的fragment，add下一个到Activity中
            } else {
                transaction.hide(mContentFragment).show(to).commitAllowingStateLoss(); // 隐藏当前的fragment，显示下一个
            }
            mContentFragment = to;
        }
    }

    @Override
    public void onTabSelected(int index) {
        switchFragment(HomeTabManager.getInstance().getFragmentByIndex(index), index);
        mIndex = index;
        setTabsDisplay(mIndex);
    }

}